package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //查询所有地址
    @GetMapping("/list")
    public R<List<AddressBook>> list(){

        //这里要先获取目前登录用户的id，用线程获取
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(userId != null,AddressBook::getUserId,userId);

        queryWrapper.orderByDesc(AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(queryWrapper);

        return R.success(list);
    }

    //新增地址
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){

        //设置当前用户的id
        Long currentId = BaseContext.getCurrentId();

        addressBook.setUserId(currentId);

        addressBookService.save(addressBook);

        return R.success("新增成功");

    }

    //回显，根据当前地址id查询地址信息
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook == null){
            throw new CustomException("地址信息不存在");
        }

        return R.success(addressBook);
    }


    //修改地址
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }

    //设为默认地址
    @PutMapping("/default")
    public R<String> updateDefault(@RequestBody AddressBook addressBook){

        //这里要保证当前用户的所有地址中只有一个地址被设置为默认地址
        //获取当前用户ID

        Long userId = BaseContext.getCurrentId();


        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();

        updateWrapper.set(AddressBook::getIsDefault,0).eq(AddressBook::getUserId,userId);

        addressBookService.update(updateWrapper);

        addressBook.setIsDefault(1);

        addressBookService.updateById(addressBook);

        return R.success("设置为默认成功");
    }

    //删除单个地址
    @DeleteMapping
    public R<String> delete(Long ids){

        addressBookService.removeById(ids);

        return R.success("删除成功");
    }

    //获取默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault(){

        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        //当前用户
        queryWrapper.eq(userId != null, AddressBook::getUserId, userId);

        //isDefault为1即默认地址
        queryWrapper.eq(AddressBook::getIsDefault,1);

        AddressBook address = addressBookService.getOne(queryWrapper);

        return R.success(address);
    }

}
